package net.zjitc.dao.impl;

import net.zjitc.dao.OrderDao;
import net.zjitc.pojo.Order;
import net.zjitc.pojo.Orderitem;
import net.zjitc.utils.db.C3P0Utils;
import org.apache.commons.dbutils.QueryRunner;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by Administrator on 2017/12/14.
 */
public class OrderDaoImpl implements OrderDao {
    QueryRunner runner = new QueryRunner();
    public void add(Order order) {
        String sql = "insert into orders value(?,?,?,?,?,?,?,?)";
        Connection conn = null;
        try{
            conn = C3P0Utils.getConnection();
            //插入订单
            Object[] obj =new Object[]{order.getOid(),order.getTotal(),order.getOrdertime(),
                    order.getState(),order.getName(),order.getPhone(),order.getAddr(),order.getUser().getUid()};
            int count = runner.update(conn,sql,obj);
            sql = "insert into orderitem(count,subtotal,pid,oid,osatate) values(?,?,?,?,?)";
            List<Orderitem> list = order.getOrderitemList();
            int len = list.size();

            Object[][] objs = new Object[len][];
            for (int i = 0; i <len ; i++) {
                Orderitem item = list.get(i);
                objs[i] = new Object[]{item.getCount(),item.getSubtotal(),item.getProduct().getPid(),order.getOid(),item.getOsatate()};
            }
            runner.batch(conn,sql,objs);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
